import sys, os

pwd = os.path.dirname(os.path.realpath(__file__))  # 获取当前脚本文件目录
sys.path.append(pwd + "../")  # pwd目录添加到系统检索目录
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'shopping.settings')  # 套路
import django

django.setup()

from goods.models import Goods,GoodsImage,GoodsCategory
from db_tools.data.product_data import row_data

for data in row_data:
    gs = Goods()
    gs.name = data['name']
    gs.market_price = float(int(data['market_price'].replace('￥','').replace('元','')))
    gs.sale_price = float(int(data['sale_price'].replace('￥','').replace('元','')))
    gs.desc = data['desc'] if data['desc'] is  not None else ''
    gs.goods_desc = data['goods_desc'] if data['goods_desc'] is not None else ''
    gs.goods_front_image = data['images'][0] if data['images'] else ''

    categorys_name = data['categorys'][-1]
    #取出对应的类别
    gc = GoodsCategory.objects.filter(name=categorys_name)
    if gc:
        gs.category = gc[0]
    #保存
    gs.save()

    image = data['images']
    for i in image:
        gi = GoodsImage()
        gi.image = i
        gi.goods = gs
        gi.save()

